package com.sfx.utils;

import com.sfx.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import static com.sfx.constants.SystemConstants.USER_TYPE_ADMIN;

public class SecurityUtils
{

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return loginUser;
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin(){
        return getLoginUser().getUser().getType().equals(USER_TYPE_ADMIN);
    }

    public static Long getUserId() {
      return getLoginUser().getUser().getId();
    }
}